package com.qiguliuxing.dts.db.service;

import com.github.pagehelper.PageHelper;
import com.qiguliuxing.dts.db.dao.GmDocMapper;
import com.qiguliuxing.dts.db.domain.GmDoc;
import com.qiguliuxing.dts.db.domain.GmDoc.Column;
import com.qiguliuxing.dts.db.domain.GmDocExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
@Service
public class GmDocService {
    private static final Logger logger = LoggerFactory.getLogger(GmDocService.class);
  Column[] columns = new Column[] { Column.id, Column.docNumber, Column.docLink, Column.docName, Column.addTime, Column.docType,Column.reviewTime,Column.certifyTime,Column.keywords,Column.iseffective,Column.deleted};
    @Resource
    private GmDocMapper docMapper;


    /**
     * 获取热卖商品
     *
     * @param offset
     * @param limit
     * @return
     */
    public List<GmDoc> queryById(int offset, int limit) {
        GmDocExample example = new GmDocExample();
        example.or().andIseffectiveEqualTo(true);
        example.setOrderByClause("add_time desc");
        PageHelper.startPage(offset, limit);

        return docMapper.selectByExampleSelective(example, columns);
    }

    /**
     * 获取新品上市
     *
     * @param offset
     * @param limit
     * @return
     */
    public List<GmDoc> queryByNew(int offset, int limit) {
        GmDocExample example = new GmDocExample();
        example.or().andIseffectiveEqualTo(true);
        example.setOrderByClause("add_time desc");
        PageHelper.startPage(offset, limit);

        return docMapper.selectByExampleSelective(example, columns);
    }

    /**
     * 获取分类下的商品
     *
     * @param catList
     * @param offset
     * @param limit
     * @return
     */
    public List<GmDoc> queryByid(List<Integer> catList, int offset, int limit) {
        GmDocExample example = new GmDocExample();
        example.or().andIdIn(catList).andIseffectiveEqualTo(true);
        example.setOrderByClause("sort_order  asc");
        PageHelper.startPage(offset, limit);

        return docMapper.selectByExampleSelective(example, columns);
    }

    /*/**
     * 获取分类下的商品
     *
     * @param catId
     * @param offset
     * @param limit
     * @return
     */
/*
*/
    /*
    public List<GmDoc> queryByCategory(Integer catId, int offset, int limit) {
        GmDocExample example = new GmDocExample();
        example.or().andCategoryIdEqualTo(catId).andIsOnSaleEqualTo(true).andDeletedEqualTo(false);
        example.setOrderByClause("add_time desc");
        PageHelper.startPage(offset, limit);

        return docMapper.selectByExampleSelective(example, columns);
    }
*/

  /*  public List<GmDoc> querySelective(int id, String docNumber, String docLink, int docType,
                                      int offset, int limit, String sort,String order) {
        GmDocExample example = new GmDocExample();
        GmDocExample.Criteria criteria1 = example.createCriteria();
//        GmDocExample.Criteria criteria2 = example.createCriteria();

        if (!StringUtils.isEmpty(id) && id != 0) {
            criteria1.andIdEqualTo(id);
//            criteria2.andIdEqualTo(id);
        }
        if (StringUtil.isNotEmpty(docNumber)) {
            criteria1.andDocNumberEqualTo(docNumber);
//            criteria2.andDocNumberEqualTo(docNumber);
        }
        if (!StringUtils.isEmpty(docLink)) {
            criteria1.andDocLinkEqualTo(docLink);
//            criteria2.andDocLinkEqualTo(docLink);
        }
        if (!StringUtils.isEmpty(docType)) {
            criteria1.andDocTypeEqualTo(docType);
//            criteria2.andDocTypeEqualTo(docType);
        }

        criteria1.andIseffectiveEqualTo(true);
//        criteria2.andIseffectiveEqualTo(true);

//        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
//            example.setOrderByClause(sort + " " + order);
//        }

        PageHelper.startPage(offset, limit);
        return docMapper.selectByExample(example);
    }*/

    public List<GmDoc> querySelective(String docNumber, String docName, Integer page, Integer size, String sort,
                                         String order) {
        GmDocExample example = new GmDocExample();
        GmDocExample.Criteria criteria = example.createCriteria();

       if (!StringUtils.isEmpty(docNumber)) {
           criteria.andDocNumberEqualTo(docNumber);
        }
//        if (!StringUtils.isEmpty(name)) {
//            criteria.andNameLike("%" + name + "%");
//        }
//        criteria.andDeletedEqualTo(false);
//
//        if (brandIds != null && brandIds.size() > 0) {
//            criteria.andBrandIdIn(brandIds);
//        }
        if (!StringUtils.isEmpty(docName)) {
            criteria.andDocNameLike("%" + docName + "%");
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return docMapper.selectByExample(example);
    }

    /**
     * 获取某个文档,包含完整信息
     *
     * @param id
     * @return
     */
    public GmDoc findById(Integer id) {
        GmDocExample example = new GmDocExample();
        /*example.or().andidEqualTo(id).andDeletedEqualTo(false);*/
        example.or().andIdEqualTo(id);
       /* return goodsMapper.selectOneByExampleWithBLOBs(example);*/
        return docMapper.selectOneByExampleSelective(example);
    }

    /**
     * 根据文档编号找到商品
     * @param docNumber
     * @return
     */
    public GmDoc findByDoctype(String docNumber) {
        GmDocExample example = new GmDocExample();
        /*example.or().andGoodsSnEqualTo(goodsSn).andDeletedEqualTo(false);*/
        example.or().andDocNumberEqualTo(docNumber).andIseffectiveEqualTo(false);
        return docMapper.selectOneByExampleSelective(example);
    }

    /**
     * 获取某个商品信息，仅展示相关内容
     *
     * @param id
     * @return
     */
    public GmDoc findByIdVO(Integer id) {
        GmDocExample example = new GmDocExample();
        example.or().andIdEqualTo(id).andIseffectiveEqualTo(true);
        return docMapper.selectOneByExampleSelective(example, columns);
    }

    public GmDoc findBySnVO(int docType) {
        GmDocExample example = new GmDocExample();
        example.or().andDocTypeEqualTo(docType).andIseffectiveEqualTo(true);
        return docMapper.selectOneByExampleSelective(example, columns);
    }

    /**
     * 获取所有在售物品总数
     *
     * @return
     */
    public Integer queryOnSale() {
        GmDocExample example = new GmDocExample();
        example.or().andIseffectiveEqualTo(true);
        return (int) docMapper.countByExample(example);
    }

    public int updateById(GmDoc doc) {
        doc.setUpdateTime(LocalDate.now());
        return docMapper.updateByPrimaryKeySelective(doc);
    }

    public void deleteById(Integer id) {
        docMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(GmDoc doc) {
       /* doc.setAddTime(""LocalDate.now()"");*/
        /*doc.setUpdateTime(LocalDateTime.now());*/
        docMapper.insertSelective(doc);
    }

    /**
     * 获取所有物品总数，包括在售的和下架的，但是不包括已删除的商品
     * 获取所有文档总数。
     * @return
     */
    public int count() {
        GmDocExample example = new GmDocExample();
        example.or();
        return (int) docMapper.countByExample(example);
    }

/*    public List<Integer> getCatIds(Integer brandId, String keywords, Boolean isHot, Boolean isNew) {
        GmDocExample example = new GmDocExample();
        GmDocExample.Criteria criteria1 = example.or();
        GmDocExample.Criteria criteria2 = example.or();

        if (!StringUtils.isEmpty(brandId)) {
            criteria1.andBrandIdEqualTo(brandId);
            criteria2.andBrandIdEqualTo(brandId);
        }
        if (!StringUtils.isEmpty(isNew)) {
            criteria1.andIsNewEqualTo(isNew);
            criteria2.andIsNewEqualTo(isNew);
        }
        if (!StringUtils.isEmpty(isHot)) {
            criteria1.andIsHotEqualTo(isHot);
            criteria2.andIsHotEqualTo(isHot);
        }
        if (!StringUtils.isEmpty(keywords)) {
            criteria1.andKeywordsLike("%" + keywords + "%");
            criteria2.andNameLike("%" + keywords + "%");
        }
        criteria1.andIsOnSaleEqualTo(true);
        criteria2.andIsOnSaleEqualTo(true);
        criteria1.andDeletedEqualTo(false);
        criteria2.andDeletedEqualTo(false);

        List<GmDoc> goodsList = goodsMapper.selectByExampleSelective(example, Column.categoryId);
        List<Integer> cats = new ArrayList<Integer>();
        for (GmDoc goods : goodsList) {
            cats.add(goods.getCategoryId());
        }
        return cats;
    }*/

    public boolean checkExistByName(String name) {
        GmDocExample example = new GmDocExample();
        example.or().andDocNameEqualTo(name);
        return docMapper.countByExample(example) != 0;
    }

    /**
     * 根据店铺，获取店铺对应类别的商品
     * 根据文档类型，获取对应文档类型的文档
     * @param brandId
     * @param i
     * @param related
     * @return
     */
/*    public List<GmDoc> queryByBrandId(int bid, int cid, int offset, int limit) {
        GmDocExample example = new GmDocExample();
        example.or().andBrandIdEqualTo(bid).andCategoryIdEqualTo(cid).andIsOnSaleEqualTo(true).andDeletedEqualTo(false);
        example.setOrderByClause("browse desc");
        PageHelper.startPage(offset, limit);

        return docMapper.selectByExampleSelective(example, columns);
    }*/

    /**
     * 同类商品，且不同店铺
     *
     * @param brandId
     * @param cid
     * @param i
     * @param limitCid
     * @return
     */
  /*  public List<GmDoc> queryByCategoryAndNotSameBrandId(int bid, int cid, int offset, int limit) {
        GmDocExample example = new GmDocExample();
        example.or().andBrandIdNotEqualTo(bid).andCategoryIdEqualTo(cid).andIsOnSaleEqualTo(true)
                .andDeletedEqualTo(false);
        example.setOrderByClause("browse desc");
        PageHelper.startPage(offset, limit);

        return docMapper.selectByExampleSelective(example, columns);
    }*/
}
